/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
    
package com.googlecode.jsimplemarket.wicket.pages;
    
import com.googlecode.jsimplemarket.model.Creanta;
import com.googlecode.jsimplemarket.model.Inchiriere;
import com.googlecode.jsimplemarket.model.Plata;
import com.googlecode.jsimplemarket.model.TotalIncasari;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.service.InchiriereService;
//import com.googlecode.jsimplemarket.wicket.pages.PiataViewPage.TotalIncasariExtraView;
import com.googlecode.jsimplemarket.wicket.pages.RaportIncasariZilnicePage.TotalIncasariView;

import java.util.*;
import java.text.*;
import java.util.List;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
//import org.apache.wicket.behavior.AttributeAppender;

/**
 *
 * @author Alexandru.Codescu
 */
public class RaportTarabePageAA extends DefaultPageTemplate {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @SpringBean(name = "genericService")
    private GenericService<Inchiriere> serviceInchiriere;
    @SpringBean(name = "genericService")
    private GenericService<Creanta> serviceCreanta;
    @SpringBean(name = "genericService")
    private GenericService<Plata> servicePlata;
    @SpringBean(name = "inchiriereService")
    private InchiriereService inchiriereService;

    private Date dataRaport = new Date();
    private Locale locale = new Locale("ro","RO");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm", locale);
    //SimpleDateFormat("dd.MM.yyyy HH:mm").format(data)
    
    public RaportTarabePageAA() {

        class TarabeView extends ListView<Object[]> {

            /**
            * 
            */
            private static final long serialVersionUID = 1L;

            public TarabeView(String id, List<Object[]> data) {
                super(id, data);
            }

            @Override
            protected void populateItem(ListItem<Object[]> li) {
                final Object[] object = li.getModelObject();
                //int index = li.getIndex() + 1;
                
                Link lnkTaraba = new Link("lnkTaraba") {
                    @Override
                    public void onClick() {
                        setResponsePage(new TarabaViewPage((Long) object[2]));
                    }
                };
                
                String lblText = object[3]==null ? "-" : object[3].toString();
                                
                lnkTaraba.add(new Label("denumire", object==null ? "-" : lblText));
                li.add(lnkTaraba);
                //li.add(new Label("denumire",object[3]==null ? "-" : object[3].toString()));
                
                
                li.add(new Label("client", object[5]==null ? "---" : object[5].toString()));
                li.add(new Label("dataPlata", object[7]==null ? "---" : DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(object[7])));
                li.add(new Label("data", object[1]==null ? "-" : DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(object[1])));
                li.add(new Label("taxa",object[6]==null ? "-" : fmt.format(object[6])));
                
                if (object[0]!=null){
                    Link delLink = new Link("delLink") {
                        @Override
                        public void onClick() {

                            Inchiriere inchiriere = serviceInchiriere.getById(Inchiriere.class, Long.valueOf(object[0].toString()));
                            Creanta creanta = inchiriere.getCreanta();
                            List<Plata> plati = creanta.getPlati();
                            for (Plata plata : plati) {
                                servicePlata.delete(plata);
                            }
                            serviceCreanta.delete(creanta);
                            serviceInchiriere.delete(inchiriere);                   
                            setResponsePage(new RaportTarabePageAA());
                        }
                    };
                    li.add(delLink);
                } else {
                    Link delLink = new Link("delLink") {
                        @Override
                        public void onClick() {
                            setResponsePage(new RaportTarabePage());
                        }
                    };
                    li.add(delLink);
                }

            }
        }
        
        class TotalIncasariView extends ListView<TotalIncasari> {
            
            public TotalIncasariView(String id, List<TotalIncasari> list) {
                super(id, list);
            }

            @Override
            protected void populateItem(ListItem<TotalIncasari> li) {
                int index = li.getIndex() + 1;
                TotalIncasari raport = li.getModelObject();
                Label lblValoareTotala = new Label("valTotala", raport.getValoareTotala()==null ? "0" : fmt.format(raport.getValoareTotala())); 
                li.add(lblValoareTotala);
            }
        }
        
        class TotalIncasariExtraView extends ListView<Object[]> {

            public TotalIncasariExtraView(String id, List<Object[]> data) {
                super(id, data);
            }

            @Override
            protected void populateItem(ListItem<Object[]> li) {
                Object[] object = li.getModelObject();
                Label lblToatalTarabe = new Label("totalTarabeExtra", object==null ? "0" : object[1].toString());
                Label lblValoareTotala = new Label("valTotalaExtra", object[0]==null ? "0" : fmt.format(object[0]));
                li.add(lblValoareTotala,lblToatalTarabe);
            }
        }

        final Form<RaportTarabePageAA> form = new Form<RaportTarabePageAA>("form");
        form.setModel(new CompoundPropertyModel<RaportTarabePageAA>(this));
        
        final DateTextField tfDataRaport = new DateTextField("dataRaport", new PropertyModel<Date>(this, "dataRaport"), "dd.MM.yyyy");
        tfDataRaport.add(new DatePicker());
        form.add(tfDataRaport);        
        
//        final Label labelTest = new Label("labelTest", dataRaport);
//        labelTest.setOutputMarkupId(true);
//        form.add(labelTest);
        
        final TarabeView raportView = new TarabeView("raport", inchiriereService.queryIncasariByDataPlataDetailed(dataRaport));        
        final TotalIncasariExtraView totalExtraView = new TotalIncasariExtraView("raportTotalExtra", inchiriereService.queryTotalIncasariByDataPlata(dataRaport));
        //final TotalIncasariExtraView totalView = new TotalIncasariExtraView("raportTotal", inchiriereService.queryTotalIncasariByData(new Date()));
       
        final WebMarkupContainer wkRaport = new WebMarkupContainer("wkRaport");
        wkRaport.setOutputMarkupId(true);
        wkRaport.setOutputMarkupPlaceholderTag(true);
        wkRaport.add(raportView, totalExtraView);
        
        form.add(wkRaport);
        form.add(new AjaxSubmitLink("submit") {

            @Override
            public void onSubmit(AjaxRequestTarget target, Form form) {
                super.onSubmit(target, form);
                tfDataRaport.processInput();
                
                raportView.setList(inchiriereService.queryIncasariByDataPlataDetailed(tfDataRaport.getConvertedInput()));
                raportView.modelChanged();

                totalExtraView.setList(inchiriereService.queryTotalIncasariByDataPlata(tfDataRaport.getConvertedInput()));
                totalExtraView.modelChanged();
                
                target.add(wkRaport);
            }
            
        });
        add(form);

    }
}